/*
 * Copyright (C) 2011 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.ui_v4;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.network.ApiResponse;
import com.mol.market.common.network.MarketAPI;
import com.mol.market.common.util.Utils;
import com.mol.market.common.vo.RecommendTopic;
import com.mol.market.ui.core.LazyLoadAdapter;

/**
 * 玩家推荐列表页
 * 
 * @author andrew.wang
 * @date 2011-9-5
 * @since Version 0.8
 * 
 */
public class MasterRecommendListFragment extends SimpleLazyLoadFragmentLoader
        implements AdapterView.OnItemClickListener {

    private int mClickItemPos;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO
//        if (resultCode == RESULT_OK) {
//
//            if (data == null) {
//                return;
//            }
//            RecommendTopic mTopic = (RecommendTopic) data
//                    .getSerializableExtra(Constants.EXTRA_RECOMMEND_DETAIL);
//
//            if (mTopic == null) {
//                return;
//            }
//            @SuppressWarnings("unchecked")
//            HashMap<String, Object> item = (HashMap<String, Object>) mAdapter
//                    .getItem(mClickItemPos);
//
//            if (item == null) {
//                return;
//            }
//            item.put(Constants.KEY_LIKE, "" + mTopic.up);
//            item.put(Constants.KEY_DISLIKE, "" + mTopic.down);
//            mAdapter.notifyDataSetChanged();
//        }
    }

    @Override
    protected void load() {
        getLoaderManager().restartLoader(MarketAPI.ACTION_GET_MASTER_RECOMMEND, null, this);
    }

    @Override
    protected LazyLoadAdapter initLazyLoadAdapter() {
        LazyLoadAdapter mAdapter = new LazyLoadAdapter(this,
                null,
                R.layout.activity_recommendation_list_item,
                new String[] {
                        Constants.KEY_ICON_URL,
                        Constants.KEY_RECOMMEND_NAME,
                        Constants.KEY_UPDATE_TIME,
                        Constants.KEY_FANS },
                new int[] {
                        R.id.iv_user_icon,
                        R.id.tv_recommendation_name,
                        R.id.tv_recommendation_time,
                        R.id.tv_fans });
        getAbsListView().setOnItemClickListener(this);
        return mAdapter;
    }

    @Override
    public Loader<ApiResponse> onCreateLoader(int id, Bundle args) {

        if (id == MarketAPI.ACTION_GET_MASTER_RECOMMEND) {
            return MarketAPI.initMaterRecommendLoader(getActivity(), getSize(), getStart());
        }
        return null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mClickItemPos = position;
        HashMap<String, Object> item = (HashMap<String, Object>) getAdapter().getItem(mClickItemPos);
        RecommendTopic topic = Utils.mapToTopic(item);
        Utils.trackEvent(getApplicationContext(), Constants.GROUP_4,
                String.format(Constants.EVENT_CLICK_MASTER, topic.title));
        Utils.gotoMaster(getActivity(), topic);
    }
    
    @Override
    protected void onBusinesLogicError(int method) {
        // 暂无推荐
        HashMap<String, Object> noData = new HashMap<String, Object>();
        noData.put(Constants.KEY_DISCUSS_CONTENT, getString(R.string.hint_no_comments));
        getAdapter().addData(noData);
        loadSuccess();
    }

    @Override
    protected ArrayList<HashMap<String, Object>> getDataFromResult(int method, Object obj) {
        return (ArrayList<HashMap<String, Object>>)
                ((HashMap<String, Object>) obj).get(Constants.KEY_RECOMMEND_LIST);
    }
}